package com.ml.mall.service.sys.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ml.mall.bean.req.sys.SysRoleEditDto;
import com.ml.mall.bean.req.sys.SysRoleNewDto;
import com.ml.mall.bean.req.sys.SysRolePermissionDto;
import com.ml.mall.bean.req.sys.SysRoleQueryDto;
import com.ml.mall.entity.sys.SysRole;
import com.ml.mall.enums.CommonEnum;
import com.ml.mall.enums.SysSeqNameEnum;
import com.ml.mall.exception.BizException;
import com.ml.mall.mapper.sys.SysRoleMapper;
import com.ml.mall.mapper.sys.SysRolePermissionMapper;
import com.ml.mall.mapper.sys.SysRoleUserMapper;
import com.ml.mall.security.JwtUtil;
import com.ml.mall.service.common.CommonSeqService;
import com.ml.mall.service.sys.ISysRolePermissionService;
import com.ml.mall.service.sys.ISysRoleService;
import com.ml.mall.service.sys.SysPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 用户角色表 服务实现类
 * </p>
 *
 * @author kyyang
 * Created 2021-01-01
 */
@Service
@Slf4j
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private CommonSeqService commonSeqService;

    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;

    @Autowired
    private SysRolePermissionMapper sysRolePermissionMapper;

    @Autowired
    private SysPermissionService sysPermissionService;

    @Autowired
    private ISysRolePermissionService sysRolePermissionService;

    @Override
    public IPage<SysRole> sysRoleByPage(SysRoleQueryDto sysRoleQueryDto) {
        IPage<SysRole> page = new Page<>(sysRoleQueryDto.getPageIndex(), sysRoleQueryDto.getPageNum());
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(sysRoleQueryDto.getRoleType() != null, SysRole::getRoleType, sysRoleQueryDto.getRoleType());
        return sysRoleMapper.selectPage(page, wrapper);
    }

    @Override
    @Transactional
    public int sysRoleNew(SysRoleNewDto sysRoleNewDto) {
        //角色名称存在时抛出异常提示
        if (countByName(sysRoleNewDto.getName()) > 0) {
            throw new BizException(CommonEnum.ROLE_NAME_ALREADY_EXISTS);
        }

        //添加角色
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleNewDto, sysRole);
        sysRole.setNum(commonSeqService.nextSeq(SysSeqNameEnum.SEQ_ROLE.getCode()).intValue());
        sysRole.setCreator(JwtUtil.getCurrentUser().getAcc());
        int result = sysRoleMapper.insert(sysRole);
        log.info("insert===roleId==={}", sysRole.getId());

        //添加权限
        SysRolePermissionDto sysRolePermissionDto = new SysRolePermissionDto();
        sysRolePermissionDto.setRoleId(sysRole.getId());
        sysRolePermissionDto.setPermissionIds(sysRoleNewDto.getPermissionIds());
        sysRolePermissionService.sysRolePermissionNew(sysRolePermissionDto);
        return result;
    }

    private Integer countByName(String name) {
        LambdaQueryWrapper<SysRole> nameQueryWrapper = new LambdaQueryWrapper<>();
        nameQueryWrapper.eq(SysRole::getName, name);
        return count(nameQueryWrapper);
    }

    @Override
    @Transactional
    public int sysRoleEdit(SysRoleEditDto sysRoleEditDto) {
        //编辑权限
        SysRolePermissionDto sysRolePermissionDto = new SysRolePermissionDto();
        sysRolePermissionDto.setPermissionIds(sysRoleEditDto.getPermissionIds());
        sysRolePermissionDto.setRoleId(sysRoleEditDto.getId());
        sysRolePermissionService.sysRolePermissionNew(sysRolePermissionDto);

        //编辑角色
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleEditDto, sysRole);
        return sysRoleMapper.updateById(sysRole);
    }

    @Override
    @Transactional
    public int sysRoleDel(List<Integer> ids) {
        int i = sysRoleMapper.deleteBatchIds(ids);
        if(null != ids && ids.size() > 0){
            //同时删除角色用户
            sysRoleUserMapper.deleteByRoleIds(ids);
            //同时删除角色权限
            sysRolePermissionMapper.deleteByRoleIds(ids);
        }
        return i;
    }
}
